/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.wip.service.spring;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.prod.bom.dao.model.ProdBomHead;
import com.erp.prod.bom.dao.model.ProdBomLine;
import com.erp.prod.bom.dao.model.ProdBomLineCO;
import com.erp.prod.bom.service.ProdBomHeadService;
import com.erp.prod.bom.service.ProdBomLineService;
import com.erp.prod.wip.dao.ProdWipTaskMaterialDao;
import com.erp.prod.wip.dao.model.ProdWipTask;
import com.erp.prod.wip.dao.model.ProdWipTaskMaterial;
import com.erp.prod.wip.dao.model.ProdWipTaskMaterialCO;
import com.erp.prod.wip.service.ProdWipTaskMaterialService;
import com.erp.prod.wip.service.ProdWipTaskService;

@Service
@Transactional(rollbackFor=Exception.class)
public class ProdWipTaskMaterialServiceImpl implements ProdWipTaskMaterialService {

    //注入Dao
    @Autowired
    private ProdWipTaskMaterialDao prodWipTaskMaterialDao;
    @Autowired
    private ProdWipTaskService prodWipTaskService;
    @Autowired
    private ProdBomHeadService prodBomHeadService;
    @Autowired
    private ProdBomLineService prodBomLineService;
    
    @Override
    public void insertDataObject(ProdWipTaskMaterial obj) {
        this.prodWipTaskMaterialDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(ProdWipTaskMaterial obj) {
        this.prodWipTaskMaterialDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(ProdWipTaskMaterial obj) {
        this.prodWipTaskMaterialDao.insertOrUpdateDataObject(obj);
    }

    @Override
    public void deleteDataObject(ProdWipTaskMaterial obj) {
        this.prodWipTaskMaterialDao.deleteDataObject(obj);
    }

    @Override
    public List<ProdWipTaskMaterial> getDataObjects() {
        return this.prodWipTaskMaterialDao.getDataObjects();
    }

    @Override
    public ProdWipTaskMaterial getDataObject(int id) {
        return this.prodWipTaskMaterialDao.getDataObject(id);
    }

    @Override
    public ProdWipTaskMaterial getDataObject(String code) {
        return this.prodWipTaskMaterialDao.getDataObject(code);
    }

    @Override
    public List<ProdWipTaskMaterial> getDataObjects(ProdWipTaskMaterialCO paramObj) {
        return this.prodWipTaskMaterialDao.getDataObjects(paramObj);
    }

    @Override
    public List<ProdWipTaskMaterial> getDataObjects(Pages pages) {
        return this.prodWipTaskMaterialDao.getDataObjects(pages);
    }
    
    @Override
    public List<ProdWipTaskMaterial> getDataObjects(Pages pages, ProdWipTaskMaterialCO paramObj) {
        return this.prodWipTaskMaterialDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, ProdWipTaskMaterialCO paramObj) {
        return this.prodWipTaskMaterialDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<ProdWipTaskMaterial> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, ProdWipTaskMaterialCO paramObj) {
        return this.prodWipTaskMaterialDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public List<ProdWipTaskMaterial> getWipTaskMaterialListByTaskCode(Pages pages, ProdWipTaskMaterialCO paramObj) {
        return this.prodWipTaskMaterialDao.getWipTaskMaterialListByTaskCode(pages, paramObj);
    }
    
    @Override
    public void updateLineForVersion(String code) {
        this.prodWipTaskMaterialDao.updateLineForVersion(code);
    }
    
    @Override
    public String createWipTaskMateriel(String taskCode) {
        Pages pages = new Pages();
        pages.setPage(1);
        pages.setMax(100);
        
        try {
            //获取车间任务物料需求
            ProdWipTaskMaterialCO prodWipTaskMaterialCO = new ProdWipTaskMaterialCO();
            prodWipTaskMaterialCO.setTaskCode(taskCode);
            List<ProdWipTaskMaterial> prodWipTaskMaterialList = this.prodWipTaskMaterialDao.getWipTaskMaterialListByTaskCode(pages, prodWipTaskMaterialCO);
            int num = 0;
            String createFlag = "Y";
            //循环物料需求
            for(ProdWipTaskMaterial prodWipTaskMaterial: prodWipTaskMaterialList) {
                num = 1;
                if(prodWipTaskMaterial.getGetQuantity()!=null&&prodWipTaskMaterial.getGetQuantity().compareTo(BigDecimal.ZERO) == 1) {
                    createFlag = "N";
                    break;
                }
            }
            
            //如果当前物料列表已领料，则不能重新生成物料需求
            if(createFlag.equals("N")) {
                return "{\"result\": \"hint\",\"message\": \"行上需要的物料已领用，无法重新生成\"}";
            }
            
            //如果存在物料需求，则先删除后创建
            if(num==1) {
                this.prodWipTaskMaterialDao.deleteWipTaskMaterialListByTaskCode(taskCode);
            }
            
            //获取车间任务信息
            ProdWipTask prodWipTask = this.prodWipTaskService.getDataObject(taskCode);
            //获取物料bom头
            ProdBomHead prodBomHead = this.prodBomHeadService.getProdBomHeadByMaterialCode(prodWipTask.getMaterialCode());
            //获取物料bom行
            ProdBomLineCO prodBomLineCO = new ProdBomLineCO();
            prodBomLineCO.setBomHeadCode(prodBomHead.getBomHeadCode());
            
            List<ProdBomLine> prodBomLineList = this.prodBomLineService.getBomLineListByBomHeadCode(pages, prodBomLineCO);
            
            //循环获取车间任务物料需求
            for(ProdBomLine prodBomLine: prodBomLineList) {
                ProdWipTaskMaterial prodWipTaskMaterial = new ProdWipTaskMaterial();
                prodWipTaskMaterial.setMaterialCode(prodBomLine.getMaterialCode());
                prodWipTaskMaterial.setOrderNumber(prodBomLine.getOrderNumber());
                prodWipTaskMaterial.setProcedureCode(prodBomLine.getProcedureCode());
                prodWipTaskMaterial.setProducePreDays(prodBomLine.getProducePreDays());
                //计算任务需要的数量
                prodWipTaskMaterial.setQuantity(prodWipTask.getQuantity().multiply(prodBomLine.getQuantity()));
                prodWipTaskMaterial.setGetQuantity(BigDecimal.ZERO);
                prodWipTaskMaterial.setTaskCode(taskCode);
                prodWipTaskMaterial.setTaskMaterialCode(SnowFlake.getConcurrentId().toString());
                this.prodWipTaskMaterialDao.insertDataObject(prodWipTaskMaterial);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
}